﻿Rsd.define('Rsd.Faith.views.LoginPage', {
    extend: 'Rsd.container.Dialog',
    requires: ['Rsd.control.Input', 'Rsd.control.Label', 'Rsd.control.Button', 'Rsd.control.Image','Rsd.control.CheckBox'],
    xtype: 'login',
    border: 0,
    width: 850,
    height: 500,
    closeBtn: false,
    layout: 'fit',
    header:{
        content:Rsd.app.appTitle,
        height:65,
        style:{
            'fontSize':'200%',
            'lineHeight':65,
            //cursor: 'default',
            'backgroundColor':'transparent'
        },
        space:1,
        visible:true
    },
    modular:{
        value:true,
        animate:false,
        style:{backgroundImage:'url(./resources/images/bg1.jpeg)',backgroundPosition:'center',backgroundSize:'cover',opacity:1}
    },
    style:{
        backgroundImage:'url(./resources/images/loginBg.jpeg)',
        backgroundPosition:'center',
        //backgroundSize:'cover'
    },
    items: [
        {
            xtype: 'container',
            height: 240,
            margin: '50 50 0 60',
            border: false,
            width: '100%',
            style:{color:'#fff'},
            layout: 'vbox',
            items: [
                {
                    xtype: 'input',
                    inputType: 'text',
                    label: {content: '用户名', width: 80},
                    height: 60,
                    width:'100%',
                    emptyText: 'user id',
                    value: '',
                    margin: '22 30 0 0'
                },
                {
                    xtype: 'input',
                    inputType: 'password',
                    label: {content: '密码', width: 80},
                    height: 60,
                    width:'100%',
                    emptyText: 'password',
                    margin: '22 30 0 0'
                },
                {
                    xtype: 'checkbox',
                    label: { content: '记住密码', position: 'right', align: 'left',width:270},
                    height: 35,
                    margin: '5 0 0 90'
                },
                {
                    xtype: 'container',
                    layout: 'hbox',
                    margin: '5 0 0 90',
                    border: false,
                    height: 40,
                    items: [
                        {
                            xtype: 'button',
                            value: '确  定',
                            width: 100,
                            height: 40,
                            border: false,
                            handler:'login'
                        },
                        {
                            xtype: 'button',
                            value: '取  消',
                            width: 100,
                            height: 40,
                            border: false,
                            handler: 'cancel'
                        }
                    ]
                },
                {
                    xtype: 'image',
                    id: 'loading',
                    margin:'10 130 0 90',
                    label:{content:'正在验证信息,请稍等...',position:'right'},
                    src: './resources/images/loading.gif',
                    visible: false,
                    border: false,
                    height: 40
                }
            ]
        }
    ],
    constructor: function (config) {
        config == config || {};
        Rsd.apply(this, config);
        this.items[0].items[0].value = Rsd.getCookie("userid");
        this.items[0].items[1].value = Rsd.getCookie("pwd");
        this.items[0].items[2].checked = Rsd.getCookie("pwd").length > 0;

    },
    /*
    * */
    login:function login()
    {
        var me = this;
        this.items[0].items[4].show();

        var _userId = this.items[0].items[0].getValue();
        var _pwd = this.items[0].items[1].getValue();

        if(_userId.length == 0 || _pwd.length == 0)
        {
            Rsd.alert("用户名和密码不允许为空！");
            return;
        }

         Rsd.app.getService('Verification.login').requestJson( {
            userid: _userId,
            pwd:_pwd
        },function(_data)
         {
             if (_data == null) {
                 Rsd.alert('请求验证失败，请联系管理员！');
                 return false;
             }

             if (_data.success) {

                 Rsd.addCookie("userid",_userId);
                 //debugger;
                 if(me.items[0].items[2].isChecked())
                 {
                     Rsd.addCookie("pwd",_pwd);
                 }
                 else
                 {
                     Rsd.deleteCookie("pwd");
                 }
                 Rsd.addCookie("token", _data.data.token);
                 Rsd.setAppToken( _data.data.token);
                 Rsd.app.userName = _data.data.XingMing;
                 me.success(_data);
                 me.close();
             } else {
                 //debugger;
                 Rsd.alert(_data.msg);

             }
            return false;

         });

    },
    /*
    * */
    cancel:function cancel()
    {
        Rsd.showMessage("您确定关闭该窗口？",[
                {
                    btn:'确 定',
                    fn:function(){
                        window.location.href = './welcome.html';
                    }
                },
                {
                    btn:'取 消',
                    fn:function(){ }
                }
            ]
        );

    }

});
